﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Golos.BackEnd.Client;

namespace Golos.Web.handlers
{
    public class GetTagsHandler:HandlerBase
    {
        public GetTagsHandler(HttpContext httpContext)
            : base(httpContext)
        {
        }
        public override void Handle()
        {
            // TEPMLATE
            // {"президент":1,"выборы2012":2}
            try
            {
                List<string> tags = TagsClient.GetPopularTags();
                
                string json = "{";
                
                foreach (string tag in tags)
                {
                    string[] t = tag.Split(new char[] {':'});
                    string[] t2 = t[0].Split(new char[] { ';' });
                    foreach (string s in t2)
                    {
                        json += "\"" + s + "\":" + t[1] + ",";    
                    }
                }

                json = json.TrimEnd(new char[] { ',', ' ' }) + "}";

                _httpContext.Response.Write(json);
            }
            catch (Exception e)
            {
                LogClass.WriteErrorEntry(e.ToString());
                _httpContext.Response.Write("{ \"error\": \"Не удаётся получить список тэгов.\" }");
            }
        }
    }
}